import {setStore, getStore} from '@/utils/store'
import {getAllDept} from '@/api/sys/deptApi'

const departs = {
    state: {
        allDeptIdKeyMap: null
    },
    actions: {
        initDepartsCache({commit}) {
            return new Promise(resolve => {
                getAllDept().then(res => {
                    let data = res.data.data;
                    let map = {};
                    for (let i = 0; i < data.length; i++) {
                        let item = data[i]
                        map[item.id] = item;
                    }
                    departs.state.allDeptIdKeyMap = map
                    resolve()
                })
            })
        },
        getDeptById: ({commit}, depId) => {
            return new Promise(resolve => {
                let map = departs.state.allDeptIdKeyMap;
                if (!map) {
                    resolve(null)
                    return null
                }
                if (map[depId]) {
                    resolve(map[depId])
                    return
                }
                resolve(null)
            });
        },
        getDeptNameById(obj, deptId) {
            return new Promise(resolve => {
                departs.actions.getDeptById(obj, deptId).then(res => {
                    if (!res) return resolve("");
                    return resolve(res.name);
                })
            })
        }
    }
}
export default departs
